function sleep() {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, 2000);
    })
}
async function fn1(next) {
    console.log('fn1');
    setTimeout(() => {
        console.log('fn1 settimeout');

    }, 1000);
    await next();
    console.log('end fn1');
}
async function fn2(next) {
    console.log('fn2');
    await sleep();
    await next();
    console.log('end fn2');
}
async function fn3(next) {
    console.log('end fn3');
}

function compose(mids) {
    return function () {//最终组合结果函数
        //执行第0个
        return dispatch(0);
        //需要返回Promise
        function dispatch(i) {
            let fn = mids[i];
            if (!fn) {
                //执行任务结束
                return Promise.resolve();
            }
            return Promise.resolve(fn(function next() {
                return dispatch(i + 1);
            }));
        }
    }
}

compose([fn1, fn2, fn3])().then(() => console.log("任务执行结束"));